package com.dict.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.dict.service.DictionaryService;
import com.opensymphony.xwork2.ActionSupport;
import com.util.NumUtil;

@Namespace("/")
@ParentPackage("json-default")
public class DictionaryAction extends ActionSupport {
	private static final long serialVersionUID = 1507469016697778756L;
	
	@Resource
	private DictionaryService dictionaryService;

	private Map<String, Object> map;
	private Object data;
	private Map<String, Object> result = new HashMap<String, Object>();

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public Object getData() {
		return data;
	}
	
	public void setData(Object data) {
		this.data = data;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	private Map<String, String> getFormData() {
		Map<String, String> item = new HashMap<String, String>();
		
		Enumeration<String> params = ServletActionContext.getRequest().getParameterNames();
		while (params.hasMoreElements()) {
			String key = params.nextElement();
			String value = ServletActionContext.getRequest().getParameter(key);
			System.out.println("-----------" + key + " - " + value);
			item.put(key, value);
		}
		
		return item;
	}
	
	// Form data: id, page, rows
	@Action(value = "dict_getdata", results = { @Result(name = "success", type = "json", params = {
			"root", "data" }) })
	public String getItems() {
		Map<String, String> item = getFormData();
		int id = NumUtil.parseInt(item.get("id"), 0);
		int page = NumUtil.parseInt(item.get("page"), 1);
		int rows = NumUtil.parseInt(item.get("rows"), 10);
		
		if (id == 0) {
			// return map
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("total", dictionaryService.getRows());
			map.put("rows", dictionaryService.getItems(page, rows));
			data = map;
		} else {
			// return list
			data = dictionaryService.getItemByPid(id);
		}
		
		return SUCCESS;
	}

	@Action(value = "save_dict", results = { @Result(name = "success", type = "json", params = {
			"root", "result" }) })
	public String saveItem() {
		Map<String, String> params = getFormData();
		
		Map<String, Object> data = dictionaryService.addItem(params);
		if (data != null) {
			result.put("success", true);
			result.put("data", data);
		} else {
			result.put("errorMsg", "The Name(" + params.get("name") +") has existed!");
		}
		return SUCCESS;
	}
	
	@Action(value = "update_dict", results = { @Result(name = "success", type = "json", params = {
			"root", "result" }) })
	public String updateItem() {
		Map<String, String> params = getFormData();
		Map<String, Object> data = dictionaryService.updateItem(params);
		if (data != null) {
			result.put("success", true);
			result.put("data", data);
		} else {
			result.put("errorMsg", "Update item (" + params.get("name") +") error!");
		}
		return SUCCESS;
	}
	
	@Action(value = "destroy_dict", results = { @Result(name = "success", type = "json", params = {
			"root", "result" }) })
	public String destroyItem() {
		Map<String, String> params = getFormData();
		String id = params.get("id");
		boolean flag = dictionaryService.removeItem(id);
		if (flag) {
			result.put("success", true);
		} else {
			result.put("errorMsg", "Item (" + id +") not exists!");
		}
		return SUCCESS;
	}
	
	@Action(value = "search_dict", results = { @Result(name = "success", type = "json", params = {
			"root", "result" }) })
	public String searchItem() {
		Map<String, String> params = getFormData();
		
		String name = ServletActionContext.getRequest().getParameter("name");
		String value = ServletActionContext.getRequest().getParameter("value");
		
		List<Map<String, Object>> list = dictionaryService.searchItem(name, value);
		int size = list.size();
		if (size > 0) {			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("total", size);
			map.put("rows", list);
			result.put("data", map);
			result.put("success", true);
		} else {
			String msg = String.format("Not found item (%s, %s)", name, value);
			result.put("errorMsg", msg);
		}
		
		return SUCCESS;
	}
	
	@Action(value = "adjust_dict", results = { @Result(name = "success", type = "json", params = {
			"root", "data" }) })
	public String updateSort() {
		Map<String, String> params = getFormData();
		int id = NumUtil.parseInt(params.get("id"), 0);
		int sort = NumUtil.parseInt(params.get("sort"), 0);
		String path = params.get("path");
		String move = params.get("move");
		
		switch(move) {
			case "up":
				data = dictionaryService.moveUp(id, path, sort);
				break;
			case "down":
				data = dictionaryService.moveDown(id, path, sort);
				break;
		}
		
		return SUCCESS;
	}
}
